package com.demo.day717;

import java.util.Calendar;
import java.util.Map;

/**
 * @Auther: MFM
 * @Date: 2021/7/17 - 07 - 17 - 20:12
 * @version: 1.0
 */

public class Work2 {
    public static StringBuilder add(String diyi, String dier) {
        StringBuilder a = new StringBuilder(diyi);
        StringBuilder b = new StringBuilder(dier);
        StringBuilder newString = new StringBuilder();

        a.reverse();
        b.reverse();

        int min;
        StringBuilder max;

        if (a.length() < b.length()) {
            min = a.length();
            max = b;
        } else {
            min = b.length();
            max = a;
        }

        boolean flag = false;

        for (int i = 0; i < min; i++) {
            int c = (a.charAt(i) + b.charAt(i) - '0' - '0') % 10;
            if (flag) {
                c++;
                flag = false;
            }
            newString.append(c);

            //是否进一位
            if (((a.charAt(i) + b.charAt(i) - '0' - '0') / 10) > 0) {
                flag = true;
            }
        }

        for (int i = min; i < max.length(); i++) {
            if (flag) {
                int i1 = max.charAt(min) - '0' + 1;
                flag = i1 / 10 > 0;
                newString.append(i1 % 10);
            } else {
                newString.append(max.charAt(i));
            }

            if (i == max.length() - 1 && flag) {
                newString.append("1");
            }
        }
        return newString.reverse();
    }

    public static void f1(String num1, String num2) {
        /*
            1.使其中一个数为基础，使用另外一个数的每个位上的去乘以这个数
            2.这两个数都需要遍历，for循环嵌套
            3.单个数与基础数进行相乘的逻辑
                单个数和每个位上的数相乘，再加上前一位进的数
                相乘后使用Builder对象拼接各位，然后让相乘的这个数除以10的值进位
            4.其他位上的数，与基础数相乘，可以看成是单个数相乘后面拼N个零
            5.要拼接的零的个数，需要计算，是length-1-i
            6.将计算的数值和之前的数值相加
            PS:进位数值和每次相乘后的数值在一次计算完成后需要清零
         */

        StringBuilder result = new StringBuilder("0");
        //每次计算结果
        StringBuilder num = new StringBuilder();
        //定义一个变量，记录进位数据
        int add = 0;
        for (int i = num1.length() - 1; i >= 0; i--) {
            //获取要计算的数值，同时计算出要拼接几个零
            int value = num1.charAt(i) - '0';
            int zeroSize = num1.length() - 1 - i;
            for (int j = num2.length() - 1; j >= 0; j--) {
                int other = num2.charAt(j) - '0';
                int mutuality = value * other;
                num.append((mutuality + add) % 10);
                add = (mutuality + add) / 10;
            }
            if (add > 0) {
                num.append(add);
            }
            num.reverse();

            //拼接0
            for (int j = 0; j < zeroSize; j++) {
                num.append("0");
            }
            result = add(num.toString(), result.toString());
            num.delete(0, num.length());
            add = 0;
        }
        System.out.println(result);
    }

    public static void f2(String num1, String num2) {

        StringBuilder newString = new StringBuilder();
        StringBuilder num = new StringBuilder("0");
        int add = 0;
        for (int i = num1.length() - 1; i >= 0; i--) {
            int numChar1 = num1.charAt(i) - '0';
            int zeroSize = num1.length() - 1 - i;
            for (int j = num2.length() - 1; j >= 0; j--) {
                int numChar2 = num2.charAt(j) - '0';
                int value = numChar1 * numChar2 + add;
                add = value / 10;
                newString.append(value % 10);
            }
            newString.reverse();
            for (int j = 0; j < zeroSize; j++) {
                newString.append("0");
            }
            num = add(newString.toString(), num.toString());
            newString.delete(0, newString.length());
            add = 0;
        }
        System.out.println(num);
    }

    public static StringBuilder minus(String minuend, String subtraction) {
        /*
            1.被减数和减数，哪一个长 长减短是正 短减长是负
            2.统一先用长的减去短的，对位相减
            3.减不了就向前借一位该数+10再减 下一个数减就要先减一
            4.一个循环 循环内对位相减  借位用boolean类型数据
         */
        String Long = "";
        String Short = "";
        boolean flag = true;
        if (minuend.length() >= subtraction.length()) {
            if (minuend.length()==subtraction.length()){
                for (int i = 0; i < minuend.length(); i++) {
                    if (minuend.charAt(i)>subtraction.charAt(i)){
                        //正数
                        Long = minuend;
                        Short = subtraction;
                        break;
                    }else if (minuend.charAt(i)<subtraction.charAt(i)){
                        //负数
                        Long = subtraction;
                        Short = minuend;
                        flag = false;
                    }
                }
            }else {
                //正数
                Long = minuend;
                Short = subtraction;
            }
        } else {
            //负数
            Long = subtraction;
            Short = minuend;
            flag = false;
        }

        int size = Long.length() - Short.length();
        //中间结果
        int result;

        StringBuilder num = new StringBuilder();

        boolean minus = false;

        for (int i = Short.length()-1; i >= 0; i--) {
            int longNum = Long.charAt(i + size) - '0';
            int shortNum = Short.charAt(i) - '0';
            if (minus){
                longNum--;
                //借位清零
                minus = false;
            }
            if (longNum<shortNum){
                longNum+=10;
                minus = true;
            }
            result= longNum - shortNum;
            num.append(result);
            //中间变量清零
            result = 0;
        }

        for (int i = size -1 ; i >= 0 ; i--) {
            if (minus){
                int c = Long.charAt(i) - '0';
                if (c<1){
                    c+=10;
                    minus = true;
                }
                c--;
                num.append(c);

            }else {
                num.append(Long.charAt(i));
            }
        }
        num.reverse();

        if (num.charAt(0)=='0'){
            num.deleteCharAt(0);
        }

        if (!flag){
            num.insert(0,'-');
        }

        return num;
    }

    public static void  zhishu(){
        int index= 0;
        for (int i = 101; i <201 ; i++) {
            for (int j = 2; j < (int)Math.sqrt(i)+1; j++) {
                if (i%j==0){
                    break;
                }

                if (j==(int)Math.sqrt(i)){
                    System.out.println(i);
                    index++;
                }
            }
        }
        System.out.println("质数一共有"+index+"个");
    }

    public static void main(String[] args) {

    }
}
